<template>
  <div id="app">
    <h3>计算属性</h3>
    <input type="text" v-model="message" />
    <p>原来的 message: "{{ message }}"</p>
    <p>计算之后的 message: "{{ reversedMessage }}"</p>
    <p>计算之后的 message: {{ msg }}</p>
    <p>计算之后的message： {{msg1}}</p>

    <hr />
    <h3>修改计算属性</h3>
    <p>first: {{first}}</p>
    <p>second: {{second}}</p>
    <p>fullname: {{fullname}}</p>
    <button @click="change">change</button>

    <hr>

    <h3>侦听器</h3>
    <input type="text" v-model="vinput">
    <p>vinput: {{ vinput }}</p>
    <p>winput: {{ winput }}</p>

  </div>
</template>

<script>
export default {
  name: "App",
  beforeCreate: function() {
    console.log("beforeCreate");
  },
  created: function() {
    console.log("created");
  },
  mounted: function() {
    console.log("mounted");
  },
  data: function() {
    return {
      message: "Hello",
      first: "zhang",
      second: "san",
      vinput: '',
      winput: '',
    };
  },
  methods: {
    change: function() {
      this.fullname = "li si";
    }
  },
  watch: {
    vinput: function (newValue) {
      this.winput =  "这是修改后的vinput：" + newValue 
    }
  },
  computed: {
    reversedMessage: function() {
      // `this` 指向 vm 实例
      return this.message
        .split("")
        .reverse()
        .join("");
    },
    msg: function() {
      return this.message + " 你好";
    },
    msg1: function() {
      return this.message + "修改";
    },
    fullname: {
      // getter
      get: function() {
        return this.first + " " + this.second;
      },
      // setter
      set: function(newValue) {
        let names = newValue.split(" ");
        this.first = names[0];
        this.second = names[names.length - 1];
      }
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
